<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Exhibition;

class ExhibitionController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $where['type'] = (int)$request->type?$request->type:1;
        $type_name = (int)$request->type==1?'正在展览':'展览预告';
        $list = Exhibition::where($where)->paginate(10);
        return view('admin.exhibition.index',compact('list','where','type_name'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        $type_name = (int)$request->type==1?'正在展览':'展览预告';
        $type = $request->type;
        return view('admin.exhibition.add',compact('type','type_name'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->except(['_token']);
       
        //dd($data);
        $file = $request->file('pic');

        // 文件是否上传成功
        if ($file->isValid()) {
            $folder = 'pic/';
            $savepath = '/public/'.$folder;
            $filename = uploadFile($file,$savepath);
            $data['pic'] = '/storage/'.$folder.$filename;      
        }
        $flag =  Exhibition::create($data);
        if($flag) {
            $rs['status'] = 'success';
            $rs['msg'] = '操作成功';
            return redirect('zadmin/exhibitions?type='.$data['type'])->with('rs',$rs);
        }
        return back()->with('rs',$rs);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
